package com.xhtech.exercise.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.xhtech.exercise.R
import com.xhtech.exercise.databinding.FragmentMainBinding
import me.luochenyxh.base.base.BaseDataBindingFragment
import me.luochenyxh.base.ext.init
import me.luochenyxh.base.ext.interceptLongClick


/**
 * 时间　: 2022/5/14
 * 作者　: yuan-xh
 * 描述　:项目主页Fragment
 */
class MainFragment : BaseDataBindingFragment<FragmentMainBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.run {
            //初始化viewpager2
            mainViewpager.initMain(this@MainFragment)
            //初始化 bottomBar
            mainBottom.init {
                when (it) {
                    R.id.menu_main -> mainViewpager.setCurrentItem(0, false)
                    R.id.menu_item1 -> mainViewpager.setCurrentItem(1, false)
                    R.id.menu_item2 -> mainViewpager.setCurrentItem(2, false)
                    R.id.menu_item3 -> mainViewpager.setCurrentItem(3, false)
                    R.id.menu_mine -> mainViewpager.setCurrentItem(4, false)
                }
            }
            mainBottom.interceptLongClick(
                R.id.menu_main,
                R.id.menu_item1,
                R.id.menu_item2,
                R.id.menu_item3,
                R.id.menu_mine
            )
        }
    }

    override fun createObserver() {

    }

  private  fun ViewPager2.initMain(fragment: Fragment): ViewPager2 {
        //是否可滑动
        this.isUserInputEnabled = false
        this.offscreenPageLimit = 5
        //设置适配器
        adapter = object : FragmentStateAdapter(fragment) {
            override fun createFragment(position: Int): Fragment {
                when (position) {
                    0 -> {
                        return HomeFragment()
                    }
                    1 -> {
                        return HomeFragment()
                    }
                    2 -> {
                        return HomeFragment()
                    }
                    3 -> {
                        return HomeFragment()
                    }
                    4 -> {
                        return HomeFragment()
                    }
                    else -> {
                        return HomeFragment()
                    }
                }
            }

            override fun getItemCount() = 5
        }
        return this
    }
}